package com.young.ums.service.impl;

import com.young.common.util.CommonUtil;
import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.Page;
import com.young.common.util.StringUtils;
import com.young.ums.core.ETopic;
import com.young.ums.core.PublishSubscribeCenter;
import com.young.ums.model.Role;
import com.young.ums.service.IDataAuthService;
import com.young.ums.service.IRoleService;
import com.young.ums.util.UmsConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* 角色服务实现
* Created by imrookie on 2018-9-30.
*/
@Service("roleService")
public class RoleServiceImpl implements IRoleService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource
    PublishSubscribeCenter publishSubscribeCenter;

    @Resource(name = "dataAuthService")
    IDataAuthService dataAuthService;

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    private static final String MAPPER = "com.young.ums.mapper.role";//mapper的namespace

    //获取数据列表
    @Override
    public List<Role> getList(Role obj) {
        //权限参数处理
        dataAuthService.handleAuthorityParameter(obj);
        if (obj.getTrashFlag() == null){
            obj.setTrashFlag(UmsConstants.TrashFlag.NO);//默认查询未删除
        }
        return dataAccessService.getList(MAPPER + ".getList", obj);
    }

    //获取数据列表(分页)
    @Override
    public List<Role> getPage(Role obj, Page page) {
        //权限参数处理
        dataAuthService.handleAuthorityParameter(obj);
        if (obj.getTrashFlag() == null){
            obj.setTrashFlag(UmsConstants.TrashFlag.NO);//默认查询未删除
        }
        return dataAccessService.getList(MAPPER + ".getList", obj, page);
    }

    /**
     * 根据单位id查询角色列表
     *
     * @param companyId
     * @return
     */
    @Override
    public List<Role> getListByCompanyId(String companyId) {
        Role obj = new Role();
        obj.setCompanyId(companyId);
        return this.getList(obj);
    }

    //查询单条
    @Override
    public Role get(String id) {
        return dataAccessService.getObject(MAPPER + ".get", id);
    }

    //统计数量
    @Override
    public int count(Role obj) {
        return dataAccessService.getObject(MAPPER + ".count", obj);
    }

    //修改
    @Override
    public int update(Role obj) {
        obj.setUpdateTime(new Date());
        return dataAccessService.update(MAPPER + ".update", obj);
    }

    //批量删除
    @Override
    public int delete(String[] idArr) {
        int i = 0;
        for(String id : idArr){
            i += this.delete(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int delete(String id) {
        //向发布订阅中心发布用户删除消息
        publishSubscribeCenter.publish(ETopic.ROLE_DEL, id);
        return dataAccessService.delete(MAPPER + ".delete", id);
    }

    //插入
    @Override
    public int insert(Role obj) {
        if (StringUtils.isBlank(obj.getId())){
            obj.setId(CommonUtil.getUUID());
        }
        obj.setCreateTime(new Date());
        obj.setUpdateTime(new Date());
        obj.setTrashFlag(UmsConstants.TrashFlag.NO);//未删除
        return dataAccessService.insert(MAPPER + ".insert", obj);
    }

    //批量插入
    @Override
    public int batchInsert(List<Role> list) {
        int i = 0;
        for(Role item : list){
            i += this.insert(item);
        }
        return i;
    }

}